package com.bjpowernode.boss.mvc.config;

import com.bjpowernode.boss.mvc.interceptors.TokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMVCConfiguration implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("===========addCorsMappings===========");
        registry.addMapping("/**")                           //拦截的请求url地址   /** 表示对所有地址应用跨域
                .allowedOriginPatterns("*")                    //请求来源的域名 ， * 表示不区分来源
                .allowedMethods("GET", "POST", "OPTIONS", "DELETE")     //允许的请求方式
                .allowCredentials(true)                   //支持cookie
                .maxAge(3600)                                   // 预检请求OPTIONS的有效时间， 3600秒
                .allowedHeaders("*");
        //.allowedHeaders("operatorId","jwt");                  // 请求header中可以包含的key
    }

    /*设置拦截器*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**") //拦截所有
                .excludePathPatterns("/operator/access"); //不拦截的地址
    }
}
